﻿var lock = false;
var page = 0;
var wait = 60;

function CheckGroupPassword() {
    if ($("#Password").val() == "") {
        CastMsg("密码不能为空不能为空");
        return false;
    }
    return true;
}
function CheckGroupLoginName() {
    var username = $("#UserName").val();
    var tf = false;
    if (username == "") {
        CastMsg("用户名不能为空");
        tf = false;;
    }
    else
    {
        $.ajax({
            type: "post",
            data: { username: username },
            url: "/UserGroup/CheckUserName",
            async: false,
            success: function (data) {
                if (data.Statu == "ok") {
                    console.log(username);
                    tf=true;
                }
                if (data.Statu == "err") {
                    CastMsg(data.Msg);
                    tf=false;
                }
            },
            error: function (data) {
                console.log("phone_false4");
                CastMsg("网络连接异常");
                tf = false;
            }
        });
        console.log(tf);
        return tf;
    }
}
function CheckGroupPhone() {
    var phone = $("#Phone").val();
    var tf = false;
    console.log(phone);
    var reg = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g;
    if (phone == "") {
        CastMsg("电话号不能为空");
        tf = false;
    }
    if (!(reg.test(phone))) {
        CastMsg("电话号码格式不正确");
        tf = false;
    }
    else {
        $.ajax({
            type: "post",
            data: { phone: phone },
            url: "/UserGroup/CheckPhone",
            async: false,
            error: function (data) {
                console.log("phone_false4");
                CastMsg("网络连接异常");
                tf = false;
            },
            success:function(data)
            {
                if (data.Statu == "ok") {
                    tf = true;
                }
                if (data.Statu == "err") {
                    CastMsg("手机号重复");
                    tf = false;
                }
            }
        });
        return tf;
    }
}
function GroupPwdEditCheck() {
    if (!CheckGroupPassword()) return false;
    if (!CheckGetPassword()) return false;
}

function CheckGroupNewPassword() {
    if ($("#PasswordNew").val() != "") {
        if ($("#PasswordNew").val().length < 4) {
            CastMsg("至少输入4位密码");
            return false;
        }
    }
    if ($('#PasswordNew').val() != $('#NewPassword').val()) {
        CastMsg("两次输入密码不一致");
        $("#ConfirmInfo").css("color:red");
        return false;
    }

    return true;
}
function CheckGroupEmail() {
    if ($("#Email").val() != "") {
        var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if (!myreg.test($("#Email").val())) {
            CastMsg("邮箱格式输入错误！");
            return false;
        }
    }
    return true;
}

function UserAddCheck() {
    if (!CheckGroupPassword()) return false;
    if (!CheckGroupLoginName()) return false;
    if (!CheckGroupPhone()) return false;
}

function GroupEditCheck() {
    if (!CheckEmail()) return false;
}

//加载牛人
function LoadUser(id, SID) {
    if (lock) {
        return;
    }
    else {
        lock = true;
        $(".loadMore").text("正在加载中，请稍后");
        $.ajax({
            url: "/UserGroup/getUser",
            type: "post",
            data: { "page": page, "id": id },
            success: function (model) {
                var str = "";
                var img = "";
                var username = "";
                var data = model.Data;
                if (model.Statu == "ok") {
                    if (SID != "") {
                        for (var i = 0 ; i < data.length; i++) {
                            if (data[i].Picture == null) {
                                img = "/Images/userphoto.png";
                            }
                            else {
                                img = "/User/ShowPicture/" + data[i].ID;
                            }
                            str += '<div class="user_item" id="item_' + data[i].ID + '"><a class="cover" href="/UserGroup/UserShow/' + data[i].ID + '" style="background-image: url(\'' + img + '\');"><p style="margin-top: 134px;" class="p_user"><span class="span10">' + data[i].UserName + '</span></p></a><div class="mangaer-list"><a href="/UserGroup/UserShow/' + data[i].ID + '"><span class="span4">查看详细</span></a><a href="javascript:deleteDialog(\'/UserGroup/UserDelete/' + data[i].ID + '?sid=' + SID + '\',\'item_' + data[i].ID + '\')" style="cursor: pointer;"><span class="span3">删除</span></a><a href="/UserGroup/UserEdit/' + data[i].ID + '"><span class="span3">编辑</span></a></div></div>';
                        }
                    }
                    else {
                        for (var i = 0 ; i < data.length; i++) {
                            if (data[i].Picture == null) {
                                img = "/Images/userphoto.png";
                            }
                            else {
                                img = "/User/ShowPicture/" + data[i].ID;
                            }
                            str += '<div class="user_item" id="item_' + data[i].ID + '"><a class="cover" href="/User/PersonalShow/' + data[i].ID + '" target="_blank" style="background-image: url(\'' + img + '\');"><p style="margin-top: 138px;" class="p_user"><span class="span10">' + data[i].UserName + '</span></p></a></div>';
                        }
                    }
                    $(".lstUser").append(str);
                    lock = false;
                    page++;
                    $(".loadMore").text("下拉加载更多！");
                }
                else if (model.Statu == "end") {
                    $(".loadMore").text("没有更多数据了！");
                }
                else if (model.Statu == "err") {
                    $(".loadMore").text("网络异常");
                }
            }
        });
    }
}
function Load() {
    if ($(".lstUser").length > 0) {
        LoadUser(UserGroup_id, SID);
    }
}

function getDistrict(district_str,town_str) {
    var str = "";
    var city_id = $("#city_id").val();
    if (city_id != null) {
        $("#district_list").empty();
        $("#town_list").empty();
        $("#District").val("");
        $("#Town").val("");
        $.ajax({
            type: "post",
            url: "/Common/GetDistrictsByCID",
            data: { id: city_id },
            success: function (model) {
                if (model.Statu == "ok") {
                    var data = model.Data;
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].DistrictName == district_str) {
                            str += '<option value="' + data[i].ID + '" selected="selected">' + data[i].DistrictName + '</option>';
                        }
                        else {
                            str += '<option value="' + data[i].ID + '">' + data[i].DistrictName + '</option>';
                        }
                    }
                    $("#district_list").append(str);
                    $("#District").val($("#distruct_list").children('option:selected').text());
                    $("#district_id").val($("#district_list").children('option:selected').val());
                    getTown(town_str);
                }
                else if (model.Statu == "err") {
                    $("#district_list").empty();
                    $("#district_list").prepend("<option value='0'>空</option>");
                }
            },
            error: function (model) {
                $("#district_list").empty();
                CastMsg("网络连接异常");
            }
        });
    }

}
function getTown(town_str) {
    var disctrcict_id = $("#district_id").val();
    if (disctrcict_id != null) {
        var str = "";
        $("#town_list").empty();
        $("#Town").val("");
        $.ajax({
            type: "post",
            url: "/Common/GetTownsByDID",
            data: { id: disctrcict_id },
            success: function (model) {
                if (model.Statu == "ok") {
                    var data = model.Data;
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].TownName == town_str) {
                            str += '<option value="' + data[i].ID + '" selected="selected">' + data[i].TownName + '</option>';
                        }
                        else {
                            str += '<option value="' + data[i].ID + '">' + data[i].TownName + '</option>';
                        }
                    }
                    $("#town_list").append(str);
                    $("#Town").val($("#town_list").children('option:selected').text());
                    $("#town_id").val($("#town_list").children('option:selected').val());
                }
                else if (model.Statu == "err") {
                    $("#town_list").empty();
                    $("#town_list").prepend("<option value='0'>空</option>");
                }
            },
            error: function (model) {
                $("#town_list").empty();
                CastMsg("网络连接异常");
            }
        });
    }
}

$(document).ready(function () {
    Load();
        $(window).scroll(
        function () {
            totalheight = parseFloat($(window).height())
               + parseFloat($(window).scrollTop());
            if (($(document).height() <= totalheight)&&(typeof (UserGroup_id) != "undefined")) {
                LoadUser(UserGroup_id, SID);
            }
        });


    $("#city_list").click(function () {
        $("#City").val($("#city_list").children('option:selected').text());
        $("#city_id").val($("#city_list").children('option:selected').val());
        getDistrict("","");
    });
    $("#district_list").click(function () {
        $("#District").val($("#district_list").children('option:selected').text());
        $("#district_id").val($("#district_list").children('option:selected').val());
        getTown("");

    });
    $("#town_list").click(function () {
        $("#Town").val($("#town_list").children('option:selected').text());
        $("#town_id").val($("#town_list").children('option:selected').val());
    });
});